package com.hnjfzn.system.controller;

import com.hnjfzn.common.core.domain.R;
import com.hnjfzn.common.core.utils.StringUtils;
import com.hnjfzn.common.core.utils.bean.BeanUtils;
import com.hnjfzn.common.core.web.controller.BaseController;
import com.hnjfzn.common.core.web.domain.AjaxResult;
import com.hnjfzn.common.log.annotation.Log;
import com.hnjfzn.common.log.enums.BusinessType;
import com.hnjfzn.common.security.annotation.RequiresPermissions;
import com.hnjfzn.common.security.utils.SecurityUtils;
import com.hnjfzn.system.api.RemoteFileService;
import com.hnjfzn.system.api.domain.SysInfo;
import com.hnjfzn.system.service.ISysInfoService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/info")
public class SysInfoController extends BaseController {

    @Resource
    private ISysInfoService sysInfoService;

    @Autowired
    private RemoteFileService fileService;

    @GetMapping
    public AjaxResult sys(@RequestParam(value = "type", required = false) String type) {
        return success(sysInfoService.sys(type));
    }

    @RequiresPermissions("system:info:list")
    @GetMapping("/list")
    public AjaxResult list(SysInfo sysInfo) {
        try {
            // 获取系统信息
            List<SysInfo> list = sysInfoService.listByMap(BeanUtils.objectToMap(sysInfo, true));
            // 获取图片访问地址
            if (null != list && !list.isEmpty()) {
                list.forEach(item -> {
                    R<String> r = fileService.getUrl(item.getSysLogo(), 3600L);
                    if (r.getCode() == R.SUCCESS) {
                        item.setSysLogo(r.getData());
                    } else {
                        item.setSysLogo("");
                    }
                });
            }
            return success(list);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @RequiresPermissions("system:info:add")
    @Log(title = "系统管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysInfo sysInfo) {
        sysInfo.setSysId(null);
        sysInfo.setCreateTime(new Date());
        sysInfo.setCreateBy(SecurityUtils.getUsername());
        sysInfo.setIsThird(StringUtils.isEmpty(sysInfo.getIsThird()) ? "Y": sysInfo.getIsThird());
        sysInfo.setStatus("0");
        return sysInfoService.save(sysInfo) ? success(sysInfo) : error();
    }

    @RequiresPermissions("system:info:edit")
    @Log(title = "系统管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysInfo sysInfo) {
        SysInfo db = sysInfoService.getById(sysInfo.getSysId());
        if (null == db){
            return error("数据不存在");
        }
        BeanUtils.copyProperties(sysInfo, db, "sys_id", "createTime", "createBy", "sys_code");
        db.setUpdateBy(SecurityUtils.getUsername());
        db.setUpdateTime(new Date());
        return sysInfoService.updateById(db) ? success(sysInfo) : error();
    }
}
